//
//  ZMyOrderManagerVC.m
//  DistributorAB
//
//  Created by LZY on 2019/10/15.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "ZMyOrderManagerVC.h"
#import "ZMyOrderManagerAddressCell.h"//地址cell
#import "ZMyOrderManagerOrderCell.h"//订单cell
#import "ZOrderManagerListModel.h"

@interface ZMyOrderManagerVC () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *orderArray;
@end

@implementation ZMyOrderManagerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"预订管理";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    
    _orderArray = [NSMutableArray array];
    
    [self setupViewAction];
    
    [self requestData];
}
#pragma mark - 界面布局
- (void)setupViewAction{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNaviHeight, kScreen_Width, kScreen_Height - kNaviHeight) style:UITableViewStylePlain];
    _tableView.backgroundColor = kColorWithHex(0xf2f2f2);
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[ZMyOrderManagerAddressCell class] forCellReuseIdentifier:NSStringFromClass([ZMyOrderManagerAddressCell class])];
    [_tableView registerClass:[ZMyOrderManagerOrderCell class] forCellReuseIdentifier:NSStringFromClass([ZMyOrderManagerOrderCell class])];
    
    //空态界面展示
    KKTableViewNoDataView *viewPlaceHolder = [[KKTableViewNoDataView alloc] initWithFrame:CGRectMake((_tableView.width - 100) / 2.0, (_tableView.height - 72) / 2.0, 100, 72) image:imgname(@"tableviewPlaseholder") viewClick:^{
    }];
    viewPlaceHolder.backgroundColor = kColorWithHex(0xf2f2f2);
    _tableView.placeHolderView = viewPlaceHolder;
    
}
#pragma mark - 数据请求
- (void)requestData{
    kWeakSelf;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObjectIfNotNil:@"" forKey:@"state"];//1已预订2已取消 空:全部
    [NetWorkTool POST:CHECK_PARTS_ORDERS param:nil success:^(id dic) {
        NSArray *array = [dic objectForKeyNotNil:@"data"];
        [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            ZOrderManagerListModel *model = [ZOrderManagerListModel mj_objectWithKeyValues:obj];
            [weakSelf.orderArray addObjectNotNil:model];
        }];
        [weakSelf.tableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.orderArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    ZOrderManagerListModel *model = [self.orderArray objectAtIndexSafe:section];
    return model.parts.count + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZOrderManagerListModel *orderModel = [self.orderArray objectAtIndexSafe:indexPath.section];
    if (indexPath.row == 0) {
        ZMyOrderManagerAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZMyOrderManagerAddressCell class]) forIndexPath:indexPath];
        cell.orderModel = orderModel;
        return cell;
    }else{
        ZMyOrderManagerOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZMyOrderManagerOrderCell class]) forIndexPath:indexPath];
        BShopPartModel *partModel = [orderModel.parts objectAtIndexSafe:indexPath.row - 1];
        cell.partModel = partModel;
        return cell;
    }
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return [ZMyOrderManagerAddressCell cellHeight];
    }else {
        return [ZMyOrderManagerOrderCell cellHeight];
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *viewFooter = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 10)];
    viewFooter.backgroundColor = kColorWithHex(0xf2f2f2);
    return viewFooter;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10.0;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
